package com.emptyMe.org.controller;


import com.emptyMe.basic.utils.AjaxResult;
import com.emptyMe.org.domain.Goods;
import com.emptyMe.org.domain.Shopcart;
import com.emptyMe.org.domain.Size;
import com.emptyMe.org.domain.Type;
import com.emptyMe.org.query.GoodsQuery;
import com.emptyMe.org.query.ShopCartQuery;
import com.emptyMe.org.service.IGoodsService;
import com.emptyMe.org.service.IShopcartService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopCart")
@Api(tags = "购物车接口",description = "购物车接口详细描述")
public class ShopcartController {

    @Resource
    private IShopcartService service;

    @PutMapping
    @ApiOperation(value = "添加或修改商品",notes = "如果有id就修改，没有就添加")
    public AjaxResult addOrUpdate(@RequestBody Shopcart shopcart){
        try {
            service.SaveShopcart(shopcart);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"更新或添加失败"+e.getCause());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除购物车")
    public AjaxResult del(@PathVariable List<Long> id){
        try {
            service.DelShopcart(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败"+e.getCause());
        }
    }

    @DeleteMapping("/delShopCart")
//    @ApiOperation(value = "批量删除")
    public AjaxResult delAll(@RequestBody List<Shopcart> shopcarts){
        try {
            ArrayList<Long> longs = new ArrayList<>();
            for (Shopcart shopcart : shopcarts) {
                longs.add(shopcart.getId());
            }
            service.DelShopcart(longs);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败"+e.getCause());
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult findAll(@RequestBody ShopCartQuery query){
        PageInfo<Shopcart> info = service.findAllShopcart(query);
        return AjaxResult.me(info);
    }

}
